<?php

namespace App\Http\Controllers\api;

use App\Classify;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\InvitingAdvert;
use App\InvitingClassify;
use App\SetTopRecord;
use App\Userinfo;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\Types\Array_;

class InvitingAdvertController extends Controller
{

    public function getTypeList(Request $request){
        $types = InvitingClassify::all();

        if(count($types) > 0){
            foreach ($types as $type){
                $type->img_url = FileManager::getAliOssFile($type->img_id);
            }
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$types]);
    }

    public function getInvitingAdvert(Request $request){

        $classifyId   = !is_null($request->get('classify_id')) ? $request->get('classify_id'):0;
        $province     = $request->get('province');
        $city         = $request->get('city');
        $area         = $request->get('area');
        $type        = $request->get('type') ? $request->get('type') : "all_list";
        $keyword     =$request->get('keyword');


        if (!is_null($keyword) || $keyword != ''){

            $adverts=DB::table('inviting_advert')->whereNull("deleted_at")->where('desc','like','%'.$keyword.'%')->whereStatus(1)->get();
        }
        else {
            if ($type != "all_list") {
                $user = UserManager::isLogin($request);
                if (!$user)
                    return response()->json(['level' => 'error', 'message' => '未登录无权限查看', "data" => "no_login"]);
                $adverts = InvitingAdvert::whereUserId($user->id)->get();
            } else {
                $adverts = DB::table('inviting_advert')->whereNull('deleted_at');

                if ($classifyId != "" && $classifyId != 0) {
                    $adverts->whereInvitingClassifyId($classifyId);
                }
                if ($province != "" && !is_null($province)) {
                    $adverts = $adverts->whereProvince($province);
                }
                if ($city != "" && !is_null($city)) {
                    $adverts = $adverts->whereCity($city);
                }
                if ($area != "" && !is_null($area)) {
                    $adverts = $adverts->whereArea($area);
                }
                $adverts = $adverts->where('status',1)->orderBy("set_top","desc")->orderBy("created_at", "desc")->get();
            }
        }


        if (count($adverts)>0) {
            foreach ($adverts as $advert){
                if($advert->set_top == 1){
                    //置顶判断
                    $setTopRecord = SetTopRecord::whereAdventId($advert->id)->whereStatus(1)->where("end_time",">",date("Y-m-d H:i:s",time()))->first();
                    if(!$setTopRecord){
                        DB::table('inviting_advert')->where("id",$advert->id)->update(["set_top"=>0]);
                    }
                }

                $advert->photo_url = FileManager::getImgFromOssUrl($advert->img_id);

                $classify=Classify::whereId($advert->classify_id)->first();
                if(!$classify){
                    $advert->classify_name="该分类已被删除";
                }else{
                    $advert->classify_name=$classify->name;
                }
            }
        }
        
        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$adverts]);
    }


    public function getAdvert(Request $request){
        $id=$request->get('id');
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '未登录无权限查看',"data"=>"no_login"]);

        $advert=InvitingAdvert::whereId($id)->whereUserId($user->id)->first();
        if (!$advert)
            return response()->json(['level' => 'error','message' => '该广告已被删除或者id出错']);

                $advert->photo_url = FileManager::getImgFromOssUrl($advert->img_id);
                $thatClassify=Classify::whereId($advert->classify_id)->first();
               $classifyName=$thatClassify?$thatClassify->name : "无";
               $advert->classify_name=$classifyName;
        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$advert]);
    }



    public function getClassify(Request $request){
       $classifies=Classify::wherePid('0')->whereType('advert')->get(['id','name']);
        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$classifies]);
    }

    public function setInvitingAdvert(Request $request){
        $img_id               =$request->get('img_id');
        $province             =$request->get('province');
        $city                 =$request->get('city');
        $area                 =$request->get('area');
        $address_detail       =$request->get('address_detail');
        $phone                =$request->get('phone');
        $classify_id          =$request->get('classify_id');
        $desc                 =$request->get('desc');
        $id                   =!is_null($request->get('id'))?$request->get('id'):0;
        $user=UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level'=>'error','message'=>'请先登录','data'=>'no_login']);
        $userInfo=Userinfo::whereUserId($user->id)->first();

        if($userInfo->personal_auth !=1 && $userInfo->company_auth != 1)
            return response()->json(['level'=>'error','message'=>'会员未认证']);

        $advert=InvitingAdvert::whereUserId($user->id)->whereId($id)->first();
        if(!$advert)
             $advert=new InvitingAdvert();
        if(!is_null($user->id))$advert->user_id=$user->id;
        if(!is_null($img_id))$advert->img_id=$img_id;
        if(!is_null($province))$advert->province=$province;
        if(!is_null($city))$advert->city=$city;
        if(!is_null($area))$advert->area=$area;
        if(!is_null($address_detail))$advert->address_detail=$address_detail;
        if(!is_null($phone))$advert->phone=$phone;
        if(!is_null($classify_id))$advert->classify_id=$classify_id;
        if(!is_null($desc))$advert->desc=$desc;

        $advert->save();
        return response()->json(['level' => 'success','message' => '设置成功','data' => $advert]);
    }

    public function getMyInvitingAdvert(Request $request){
        $user=UserManager::isLogin($request);

        if(!$user)
            return response()->json(['level'=>'error','message'=>'请先登录','data'=>'no_login']);

        $myAdvert=InvitingAdvert::whereUserId($user->id)->get();

        if (count($myAdvert)>0) {
            foreach ($myAdvert as $advert){
                $advert->photo_url = FileManager::getImgFromOssUrl($advert->img_id);
            }
        }
        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$myAdvert]);

    }
    public  function delMyInvitingAdvert(Request $request){

        $advertId  =  $request->id;

        $user=UserManager::isLogin($request);

        if(!$user)
            return response()->json(['level'=>'error','message'=>'请先登录','data'=>'no_login']);
        $thatAdvert=InvitingAdvert::whereId($advertId)->whereUserId($user->id)->first();
        if(!$thatAdvert)
            return response()->json(['level' => 'error','message' => '当前id不存在！']);
        $thatAdvert->delete();
        return response()->json(['level'=>'success','message'=>'删除成功','data'=>$thatAdvert]);
    }

}
